﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Export;
using Waker.Common;

namespace Waker.Base
{
    public partial class FormBaseToolBar : Base.FormBase
    {

        protected bool AutoRefresh { get; set; }
        public FormBaseToolBar()
        {
            InitializeComponent(); 
        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormRefresh();
        } /// <summary>
        /// 刷新虚方法
        /// </summary>
        public virtual void FormRefresh()
        {
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barClose_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormClose();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        public virtual void FormClose()
        {
            this.Close();
        }


        /// <summary>
        /// 禁用工具栏选项
        /// </summary>
        /// <param name="args"></param>
        public void DisableToolButtons(params string[] args)
        {
            EnableAllToolButtons();
            for (int i = 0; i < bar1.LinksPersistInfo.Count; i++)
            {
                for (int j = 0; j < args.Length; j++)
                {
                    BarLargeButtonItem bi = (BarLargeButtonItem)bar1.LinksPersistInfo[i].Item;
                    if (bi.Tag.ToString() == args[j])
                    {
                        bi.Enabled = false;
                    }
                }
            }
        }
        public void HideToolButtons(params BarLargeButtonItem[] args)
        {
            foreach (var bi in args)
            {
                bi.Visibility = BarItemVisibility.Never;
            }
        }
        /// <summary>
        /// 开启所有工具栏
        /// </summary>
        public void EnableAllToolButtons()
        {
            for (int i = 0; i < bar1.LinksPersistInfo.Count; i++)
            {
                BarLargeButtonItem bi = (BarLargeButtonItem)bar1.LinksPersistInfo[i].Item;
                bi.Enabled = true;
            }
        }
        /// <summary>
        /// 添加工具栏
        /// </summary>
        /// <param name="index"></param>
        /// <param name="beginGroup"></param>
        /// <param name="btn"></param>
        private void InsertToolButton(int index, bool beginGroup, BarLargeButtonItem btn)
        {
            var temp = new LinkPersistInfo(btn);
            temp.BeginGroup = beginGroup;
            this.bar1.LinksPersistInfo.Insert(index, temp);
        }
        /// <summary>
        /// 激活刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseToolBarForm_Activated(object sender, EventArgs e)
        {
            if (AutoRefresh)
            {
                //if (ParentForm != null && ParentForm is FormMain)
                //{
                //    ((FormMain)ParentForm).ChangeToolBarVisible(bar1.Visible);
                //}
                FormRefresh();
            }
        }
        #region 增删改
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnAdd_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormAdd();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormEdit();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnDelte_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormDelete();
        }
        /// <summary>
        /// 添加(虚方法)
        /// </summary>
        protected virtual void FormAdd() { }
        /// <summary>
        /// 修改(虚方法)
        /// </summary>
        protected virtual void FormEdit() { }
        /// <summary>
        /// 删除(虚方法)
        /// </summary>
        protected virtual void FormDelete() { }
        #endregion

        #region 导出

        private void barBtnExportXls_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormExportXls();
        }
        protected virtual void FormExportXls()
        {
            if (this.gridControl1 != null && this.gridControl1.DataSource != null && this.gridView1 != null && this.gridView1.RowCount > 0)
                ExportExecl(this.gridView1, string.Empty);
        }
        private void barBtnExportPDF_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormExportPDF();
        }
        protected virtual void FormExportPDF()
        {
            if (this.gridControl1 != null && this.gridControl1.DataSource != null && this.gridView1 != null && this.gridView1.RowCount > 0)
                ExportPDF(this.gridView1, string.Empty);
        }
        private void barBtnExportHtml_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormExportHTML();
        }
        protected virtual void FormExportHTML()
        {
            if (this.gridControl1 != null && this.gridControl1.DataSource != null && this.gridView1 != null && this.gridView1.RowCount > 0)
                ExportHTML(this.gridView1, string.Empty);
        }
        #region 导出
        /// <summary>
        /// 从GridView控件的数据源中导出Excel
        /// </summary>
        /// <param name="Export_GridView">数据源所在的Excel控件</param>
        /// <param name="File_Name">导出后保存的文件名</param>
        public static void ExportExecl(DevExpress.XtraGrid.Views.Grid.GridView Export_GridView, string File_Name)
        {
            string fileName = ShowSaveFileDialog("微软Excel文档", "Microsoft Excel|*.xls", File_Name);
            if (fileName != "")
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;

                //BaseExportLink link = Export_GridView.CreateExportLink(new ExportXlsProvider(fileName));
                //link.ExportTo(true);
                Export_GridView.ExportToXls(fileName);
                Cursor.Current = currentCursor;
                OpenFile(fileName);
            }
        }
        public static void ExportPDF(DevExpress.XtraGrid.Views.Grid.GridView Export_GridView, string File_Name)
        {
            string fileName = ShowSaveFileDialog("PDF文档", "PDF文档|*.PDF", File_Name);
            if (fileName != "")
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                //ExportTo(new ExportXlsProvider(fileName), Export_GridView);
                Export_GridView.ExportToPdf(fileName);
                Cursor.Current = currentCursor;
                OpenFile(fileName);
            }
        }
        public static void ExportHTML(DevExpress.XtraGrid.Views.Grid.GridView Export_GridView, string File_Name)
        {
            string fileName = ShowSaveFileDialog("网页", "网页文件|*.html|网页文件|*.htm", File_Name);
            if (fileName != "")
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                //ExportTo(new ExportXlsProvider(fileName), Export_GridView);
                Export_GridView.ExportToHtml(fileName);
                Cursor.Current = currentCursor;
                OpenFile(fileName);
            }
        }
        ///// <summary>
        ///// 指定导出的目的地
        ///// </summary>
        ///// <param name="provider"></param>
        ///// <param name="Export_GridView"></param>
        //private static void ExportTo(IExportProvider provider, DevExpress.XtraGrid.Views.Base.BaseView Export_GridView)
        //{

        //}

        /// <summary>
        /// 显示保存对话框
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        protected static string ShowSaveFileDialog(string title, string filter, string filename)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "数据导出";
            if (filename == null || filename.Trim() == "")
            { dlg.FileName = "数据导出"; }
            else
            {
                dlg.FileName = filename;
            }
            dlg.Filter = filter;
            DialogResult res = dlg.ShowDialog();
            if (res == DialogResult.OK)
            {
                return dlg.FileName;
            }
            return "";
        }
        /// <summary>
        /// 打开刚才导出的文件
        /// </summary>
        /// <param name="fileName"></param>
        protected static void OpenFile(string fileName)
        {
            if (UI.ShowOKCancelReturnBool("要打开导出的文件吗?"))
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = fileName;
                process.StartInfo.Verb = "Open";
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                process.Start();
            }
        }

        #endregion

        private void barBtnPrint_ItemClick(object sender, ItemClickEventArgs e)
        {
            FormPrint();
        }
        protected virtual void FormPrint()
        {
            if (this.gridControl1 != null && this.gridControl1.DataSource != null && this.gridView1 != null && this.gridView1.RowCount > 0)
                this.gridControl1.Print();
        }
        #endregion

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        } 
    }
}
